home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6594 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: lafn.org!an234
  3. From: an234@lafn.org (Andres Lessing)
  4. Subject: Re: Finding a prime number
  5. X-Nntp-Posting-Host: lafn.org
  6. Message-ID: <1996Feb14.070305.19468@lafn.org>
  7. Sender: news@lafn.org
  8. Reply-To: an234@lafn.org (Andres Lessing)
  9. Organization: The Los Angeles Free-Net
  10. References: <DMpuHt.3yL@cwi.nl> <4fl2tl$ln6@ns2.emirates.net.ae> <4fnnfuINNib7@keats.ugrad.cs.ubc.ca> <4fp2kt$pks@oban.cc.ic.ac.uk
  11. Date: Wed, 14 Feb 1996 07:03:05 GMT
  12.  
  13.  
  14. In a previous article, dik@cwi.nl (Dik T. Winter) says:
  15.  
  16. >In article <4fp2kt$pks@oban.cc.ic.ac.uk> a.kruczkowski@ic.ac.uk (Alex Kruczkowski) writes:
  17. > > Any comments or am I way off here? ;-)
  18. >
  19. >Way off I think.  There is no repeating pattern in the list of prime
  20. >numbers.
  21. >-- 
  22. >dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098
  23. >home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/
  24. >
  25. Ok... Here is some code
  26.  
  27. #include <stdio.h>
  28. #include <conio.h>
  29.  
  30. int is_prime(int n)
  31. //return 0 if not prime 
  32. //return 1 if prime
  33. {
  34.     int i,temp=0;
  35.     for(i=2; i==n-1, n%i!=0; i++)
  36.       {
  37.       }
  38.     /* If i==n or n ==1 then n is prime */
  39.     if (i==n || n==1) temp = 1;
  40.     return(temp);
  41. }
  42.  
  43. I think this works pretty well.  I however don't know how fast it is..
  44.  
  45.  
  46. Hope it helps
  47. -- 
  48.  Hofstadter's Law:                   |       o__
  49.     It always takes longer than you expect, even   |     _.</)_
  50.     when you take Hofstadter's Law into account.   |    (_) \(_)
  51.         " Godel Escher Bach"               | Andres, an234@lafn.org
  52.